package com.stylefeng.guns.common.util;

import com.stylefeng.guns.common.bo.ImageFileBo;
import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author carloszhang
 * <p>
 * base64字符串转换成图片
 */
@Slf4j
public class Bese64ToImageUtil {


    /**
     * base64字符串转化成图片
     */
    private static final int IMAGE_SIZE = 500;

    public static boolean base64ToImageOutput(String base64ImgStr, String imgFilePath) {
        if (StringUtils.isBlank(base64ImgStr)) { //图像数据为空
            return false;
        }
        OutputStream os = null;
        try {
            //Base64解码
            byte[] b = Base64.decodeBase64(base64ImgStr);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {//调整异常数据
                    b[i] += 256;
                }
            }
            BufferedImage originalImage = ImageIO.read(new ByteArrayInputStream(b));
            if (originalImage.getWidth() > IMAGE_SIZE || originalImage.getHeight() > IMAGE_SIZE) {//大小分别大于500
                Thumbnails.of(new ByteArrayInputStream(b)).scale(0.8f).toFile(imgFilePath);
            } else {
                Thumbnails.of(new ByteArrayInputStream(b)).scale(1.0).toFile(imgFilePath);
            }
            return true;
        } catch (Exception e) {
            log.error("上传图片失败", e);
            throw new GunsException(BizExceptionEnum.UPLOAD_IMAGE_ERROR);
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
            } catch (IOException e) {
                log.error("上传图片失败", e);
                throw new GunsException(BizExceptionEnum.UPLOAD_IMAGE_ERROR);
            }
        }
    }

    /**
     *
     */
    private static final String IS_IMAGE = "data:image";

    public static ImageFileBo base64ToImageFileBo(String imageData) {
        if (StringUtils.isBlank(imageData) && !imageData.contains(IS_IMAGE)) {
            throw new GunsException(BizExceptionEnum.UPLOAD_IMAGE_TYPE_ERROR);
        }
        String dataPrix = "";
        String data = "";
        String[] split = imageData.split("base64,");
        ImageFileBo imageFileBo = new ImageFileBo();
        if (split != null && split.length == 2) {
            dataPrix = split[0];
            data = split[1];
        } else {
            throw new GunsException(BizExceptionEnum.UPLOAD_IMAGE_TYPE_ERROR);
        }
        imageFileBo.setImageData(data);
        return imageFileBo;
    }

    public static void main(String[] args) {

        String str = "";
        ImageFileBo imageFileBo = Bese64ToImageUtil.base64ToImageFileBo(str);
        Bese64ToImageUtil.base64ToImageOutput(imageFileBo.getImageData(), "/Users/carloszhang/Downloads/zktest.jpg");

    }
}
